package cn.wolfcode.controller;


import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        if (empService.login(emp) != null) {
            Map<String, Object> map = new HashMap<>() {
                {
                    this.put("username", emp.getUsername());
                    this.put("name", emp.getName());
                }
            };
            String token = JWTUtils.generateToken(map);
            return Result.success(token);
        } else {
            return Result.error("哥们，账号或密码不对，请重试！！！");
        }

    }
}
